package com.lfp.socketio;

import com.corundumstudio.socketio.SocketIOServer;
import com.lfp.socketio.listener.SocketIoListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author mos
 */
@Component
public class SocketIoRunner implements ApplicationRunner, SocketIoService {

    @Autowired
    private SocketIOServer server;

    @Autowired
    private SocketIoListener listener;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        server.start();
    }

    @PostConstruct
    @Override
    public void start() throws Exception {
        server.addConnectListener(listener);
        server.addDisconnectListener(listener);
    }

    @PreDestroy
    @Override
    public void stop() {
        server.stop();
    }
}
